package com.learn.dubbo.common.init;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.learn.dubbo.common.ConstantObject;
import com.learn.dubbo.common.dict.DictRemoteService;
import com.learn.dubbo.core.redis.Interface.JedisHash;
import com.learn.dubbo.core.redis.core.JedisUtil;
import com.learn.dubbo.entity.dict.SysDictItem;
import com.learn.dubbo.entity.dict.SysDictType;

/**
 * 容器初始化后，立刻启动的方法
 * @author Administrator
 *
 */
@Component
public class InitAfterStart {
	
	@Autowired
	private DictRemoteService dictRemoteService;
	
	/**
	 * @Title: setAllSysDictTypeToRedis 
	 * @author 林彬
	 * @Description: 字典放入redis 
	 * @date 2016年12月29日 上午11:52:52 
	 * @return void   
	 * @throws
	 */
    @PostConstruct
    public void setAllSysDictTypeToRedis(){
    	//查询所有字典类型
    	List<SysDictType> list = dictRemoteService.findAllSysDictType();
    	if(list!=null&&list.size()>0){
    		JedisHash JedisUtil = new JedisUtil();
    		Map<String,String> typemap = new HashMap<String,String>();
        	for(SysDictType obj : list){
        		typemap.put(obj.getTypeCode(), obj.getTypeName());
        		
        		//根据类型编码查询字典
        		List<SysDictItem> itemlist = dictRemoteService.findSysDictItemByTypeCode(obj.getTypeCode());
        		if(itemlist!=null&&itemlist.size()>0){
        			Map<String,String> itemmap = new HashMap<String,String>();
            		for(SysDictItem item : itemlist){
            			itemmap.put(item.getCode(), item.getName());
            		}
            		JedisUtil.hmset(obj.getTypeCode(), itemmap);
        		}
        	}
        	JedisUtil.hmset(ConstantObject.DITC_TYPE, typemap);
    	}
    }

}
